class FileSystem:
    def __init__(self, fs_path="light.fs"):
        self.fs_path = fs_path
        self.metadata_offset = 0
        self.metadata_size = 56 * 1024 * 1024  # 56MB
        self.data_offset = self.metadata_size
        self.data_size = 200 * 1024 * 1024  # 200MB
        self.block_size = 1 * 1024 * 1024  # 1MB per block
        self.num_blocks = self.data_size // self.block_size  # 200 blocks

        # 加载元数据，包括位图和文件索引表
        self.bitmap = [False] * self.num_blocks  # 假设所有块最初都是空闲的
        self.fat_table = [-1] * self.num_blocks  # 每个文件的 FAT 链表，-1 表示文件结束

    def read_block(self, block_index):
        with open(self.fs_path, 'rb') as fs:
            fs.seek(self.data_offset + block_index * self.block_size)
            return fs.read(self.block_size)

    def write_block(self, block_index, data):
        with open(self.fs_path, 'r+b') as fs:
            fs.seek(self.data_offset + block_index * self.block_size)
            fs.write(data)

    def find_free_block(self):
        for i in range(self.num_blocks):
            if not self.bitmap[i]:
                return i
        return -1  # 没有空闲块

    def allocate_block(self, block_index):
        self.bitmap[block_index] = True

    def deallocate_block(self, block_index):
        self.bitmap[block_index] = False
